//给你一个整数数组 nums，有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位
//。 
//
// 返回 滑动窗口中的最大值 。 
//
// 
//
// 示例 1： 
//
// 
//输入：nums = [1,3,-1,-3,5,3,6,7], k = 3
//输出：[3,3,5,5,6,7]
//解释：
//滑动窗口的位置                最大值
//---------------               -----
//[1  3  -1] -3  5  3  6  7       3
// 1 [3  -1  -3] 5  3  6  7       3
// 1  3 [-1  -3  5] 3  6  7       5
// 1  3  -1 [-3  5  3] 6  7       5
// 1  3  -1  -3 [5  3  6] 7       6
// 1  3  -1  -3  5 [3  6  7]      7
// 
//
// 示例 2： 
//
// 
//输入：nums = [1], k = 1
//输出：[1]
// 
//
// 
//
// 提示： 
//
// 
// 1 <= nums.length <= 10⁵ 
// -10⁴ <= nums[i] <= 10⁴ 
// 1 <= k <= nums.length 
// 
//
// Related Topics 队列 数组 滑动窗口 单调队列 堆（优先队列） 👍 2604 👎 0


package leetcode.editor.cn;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 滑动窗口最大值
 *
 * @author CodeTheory
 * @date 2023-11-16 16:50:45
 */
public class P239_SlidingWindowMaximum {
    public static void main(String[] args) {
        Solution solution = new P239_SlidingWindowMaximum().new Solution();
        int[] nums = new int[]{1, 3, -1, -3, 5, 3, 6, 7};
        int[] ints = solution.maxSlidingWindow(nums, 3);
//        int[] nums = new int[]{1, -1};
//        int[] ints = solution.maxSlidingWindow(nums, 1);
//        int[] nums = new int[]{1, 3, 1, 2, 0, 5};
//        int[] ints = solution.maxSlidingWindow(nums, 3);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] maxSlidingWindow(int[] nums, int k) {
            int[] res = new int[nums.length - k + 1];
            int index = 0;
            Deque<Integer> deque = new LinkedList<>();
            for (int i = 0; i < k; i++) {
                while (!deque.isEmpty() && deque.peekLast() < nums[i]) {
                    deque.pollLast();
                }
                deque.offer(nums[i]);
            }
            res[index++] = deque.peekFirst();
            for (int i = k; i < nums.length; i++) {
                if (deque.peekFirst() == nums[i - k]) {
                    deque.poll();
                }
                while (!deque.isEmpty() && deque.peekLast() < nums[i]) {
                    deque.pollLast();
                }
                deque.offer(nums[i]);
                res[index++] = deque.peekFirst();
            }
            return res;

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}

/*

class Solution {
        public int[] maxSlidingWindow(int[] nums, int k) {
            int[] res = new int[nums.length - k + 1];
            MyQueue myQueue = new MyQueue();
            int index = 0;
            for (int i = 0; i < k; i++) {
                myQueue.offer(nums[i]);
            }
            res[index++] = myQueue.peek();
            for (int i = k; i < nums.length; i++) {
                myQueue.poll(nums[i - k]);
                myQueue.offer(nums[i]);
                res[index++] = myQueue.peek();
            }
            return res;

        }

        class MyQueue {
            Deque<Integer> deque = new LinkedList<>();
            ArrayDeque<Integer> deque = new ArrayDeque<>();

            public void poll(int val) {
                if (!deque.isEmpty() && deque.peek() == val) {
                    deque.poll();
                }
            }

            public void offer(int val) {
                while (!deque.isEmpty() && deque.peekLast() < val) {
                    deque.pollLast();
                }
                deque.offer(val);
            }

            public int peek() {
                return deque.isEmpty() ? -1 : deque.peek();
            }

        }
    }

 */